package com.example.anni.zhuzhoujia.bogege;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 张钧博 on 2016/6/3.
 * 专题-张
 */
public class SpecialFragment extends Fragment {
    private GridView gvSpecial;

    private ImageView ivSpecialCover;
    private TextView tvSpecialTitle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.special_fragment, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        x.view().inject(getActivity());
        gvSpecial = (GridView) view.findViewById(R.id.gv_special);

        readData();


    }

    /**
     * 读取数据
     */
    private void readData() {
        RequestParams params = new RequestParams("http://tr.zzapi.gson.cn/api/subject/list?page=1&rows=50");
        params.addBodyParameter("username", "账号");
        params.addBodyParameter("password", "密码");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                final List<Map<String, String>> list = new ArrayList<Map<String, String>>();
                JSONObject jsonObject = JSON.parseObject(s);
                JSONArray datas = jsonObject.getJSONArray("data");
                for (int i = 0; i < datas.size(); i++) {
                    Map<String, String> map = new HashMap<String, String>();
                    JSONObject data = datas.getJSONObject(i);
                    String specialTitle = data.getString("title");
                    String specialCover = data.getString("cover");
                    String specialId = data.getString("id") + "";
                    //Toast.makeText(getActivity(), specialId, Toast.LENGTH_SHORT).show();

                    map.put("specialTitle", specialTitle);
                    map.put("specialCover", specialCover);
                    map.put("specialId", specialId);

                    list.add(map);
                }

                /**
                 * Gridview适配器
                 */
                gvSpecial.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return list.size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return null;
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        View root = getLayoutInflater(getArguments()).inflate(R.layout.special_gridview_item, null);
                        ivSpecialCover = (ImageView) root.findViewById(R.id.iv_special_cover);
                        tvSpecialTitle = (TextView) root.findViewById(R.id.tv_special_title);
                        ImageOptions options = new ImageOptions.Builder()
                                .setFailureDrawableId(R.mipmap.ic_launcher).build();
                        x.image().bind(ivSpecialCover, "http://tr.zzapi.gson.cn/" + list.get(position).get("specialCover"), options);
                        tvSpecialTitle.setText(list.get(position).get("specialTitle"));
                        return root;
                    }
                });

                /**
                 * Gridview的监听
                 */
                gvSpecial.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getActivity(), SpecialSeeNewsTypeActivity.class);
                        intent.putExtra("newstypeId", (CharSequence) list.get(position).get("specialId"));
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
